package puziran.sofa.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Json2ObjectUtil {
	public static Map<String, Object> parseJSON2Map(String jsonStr) {
		Map map = new HashMap();

		JSONObject json = JSONObject.fromObject(jsonStr);
		for (Iterator localIterator1 = json.keySet().iterator(); localIterator1
				.hasNext();) {
			Object k = localIterator1.next();
			Object v = json.get(k);

			if ((v instanceof JSONArray)) {
				List list = new ArrayList();
				Iterator it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = (JSONObject) it.next();
					list.add(parseJSON2Map(json2.toString()));
				}
				map.put(k.toString(), list);
			} else {
				map.put(k.toString(), v);
			}
		}
		return map;
	}

	public static List<Map<String, Object>> parseJSON2List(String jsonStr) {
		JSONArray jsonArr = JSONArray.fromObject(jsonStr);
		List list = new ArrayList();
		Iterator it = jsonArr.iterator();
		while (it.hasNext()) {
			JSONObject json2 = (JSONObject) it.next();
			list.add(parseJSON2Map(json2.toString()));
		}
		return list;
	}
}